package com.ruoyi.sbgl.sbwxjl.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbgl.sbwxjl.domain.Sbwxjl;
import com.ruoyi.sbgl.sbwxjl.domain.SbwxjlFj;
import com.ruoyi.sbgl.sbwxjl.service.ISbwxjlService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 设备维修记录Controller
 *
 * @author huacai
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/sbgl/sbwxjl")
public class SbwxjlController extends BaseController {
    @Resource
    private ISbwxjlService sbwxjlService;

    /**
     * 查询设备维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:list')")
    @GetMapping("/list")
    public TableDataInfo list(Sbwxjl sbwxjl) {
        startPage();
        List<Sbwxjl> list = sbwxjlService.selectSbwxjlList(sbwxjl);
        return getDataTable(list);
    }

    /**
     * 导出设备维修记录列表
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:export')")
    @Log(title = "设备维修记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Sbwxjl sbwxjl) {
        List<Sbwxjl> list = sbwxjlService.selectSbwxjlList(sbwxjl);
        ExcelUtil<Sbwxjl> util = new ExcelUtil<Sbwxjl>(Sbwxjl.class);
        util.exportExcel(response, list, "设备维修记录数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Sbwxjl> util = new ExcelUtil<Sbwxjl>(Sbwxjl.class);
        util.importTemplateExcel(response, "设备维修记录数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "设备维修记录", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Sbwxjl> util = new ExcelUtil<Sbwxjl>(Sbwxjl.class);
        InputStream inputStream = file.getInputStream();
        List<Sbwxjl> list = util.importExcel(inputStream);
        inputStream.close();
        int count = sbwxjlService.batchInsertSbwxjl(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取设备维修记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:query')")
    @GetMapping(value = "/{sbwxjlId}")
    public AjaxResult getInfo(@PathVariable("sbwxjlId") String sbwxjlId) {
        return success(sbwxjlService.selectSbwxjlBySbwxjlId(sbwxjlId));
    }

    /**
     * 新增设备维修记录
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:add')")
    @Log(title = "设备维修记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Sbwxjl sbwxjl) {
        sbwxjl.setUserId(Math.toIntExact(getUserId()));
        sbwxjl.setDeptId(Math.toIntExact(getDeptId()));
        sbwxjl.setCreateBy(getUsername());
        return toAjax(sbwxjlService.insertSbwxjl(sbwxjl));
    }

    /**
     * 修改设备维修记录
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:edit')")
    @Log(title = "设备维修记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Sbwxjl sbwxjl) {
        return toAjax(sbwxjlService.updateSbwxjl(sbwxjl));
    }

    /**
     * 删除设备维修记录
     */
    @PreAuthorize("@ss.hasPermi('sbgl:sbwxjl:remove')")
    @Log(title = "设备维修记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{sbwxjlIds}")
    public AjaxResult remove(@PathVariable String[] sbwxjlIds) {
        return toAjax(sbwxjlService.deleteSbwxjlBySbwxjlIds(sbwxjlIds));
    }

    /**
     * 获得设备维修记录ID (UUID)
     */
    @GetMapping("/getSbwxjlId")
    public AjaxResult getSbwxjlId(){
        String sbwxjlId = IdUtils.fastSimpleUUID();
        return success(sbwxjlId);
    }

    /**
     * 新增设备维修记录附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertSbwxFjBySbwxjlId(@RequestBody SbwxjlFj sbwxjlFj){
        return toAjax(sbwxjlService.insertSbwxFjBySbwxjlId(sbwxjlFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectSbwxFjList(String sId){
        SbwxjlFj sbwxjlFj = new SbwxjlFj();
        sbwxjlFj.setSbwxjlId(sId);
        return AjaxResult.success(sbwxjlService.selectSbwxFjList(sbwxjlFj));
    }

    /**
     * 删除设备维修记录附件
     */
    @Log(title = "设备维修记录附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult remove(@PathVariable Integer[] fjIds)
    {
        return toAjax(sbwxjlService.deleteSbwxjlFjByFjIds(fjIds));
    }
}
